<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>6.4. textwrap — Text wrapping and filling &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="6. Text Processing Services" href="text.html" />
    <link rel="next" title="6.5. unicodedata — Unicode Database" href="unicodedata.html" />
    <link rel="prev" title="6.3. difflib — Helpers for computing deltas" href="difflib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="unicodedata.html" title="6.5. unicodedata — Unicode Database"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="difflib.html" title="6.3. difflib — Helpers for computing deltas"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="text.html" accesskey="U">6. Text Processing Services</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-textwrap">
<span id="textwrap-text-wrapping-and-filling"></span><h1>6.4. <a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><tt class="xref py py-mod docutils literal"><span class="pre">textwrap</span></tt></a> &#8212; Text wrapping and filling<a class="headerlink" href="#module-textwrap" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.4/Lib/textwrap.py">Lib/textwrap.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-textwrap" title="textwrap: Text wrapping and filling"><tt class="xref py py-mod docutils literal"><span class="pre">textwrap</span></tt></a> module provides some convenience functions,
as well as <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a>, the class that does all the work.
If you&#8217;re just wrapping or filling one or two text strings, the convenience
functions should be good enough; otherwise, you should use an instance of
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> for efficiency.</p>
<dl class="function">
<dt id="textwrap.wrap">
<tt class="descclassname">textwrap.</tt><tt class="descname">wrap</tt><big>(</big><em>text</em>, <em>width=70</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#textwrap.wrap" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most
<em>width</em> characters long.  Returns a list of output lines, without final
newlines.</p>
<p>Optional keyword arguments correspond to the instance attributes of
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a>, documented below.  <em>width</em> defaults to <tt class="docutils literal"><span class="pre">70</span></tt>.</p>
<p>See the <a class="reference internal" href="#textwrap.TextWrapper.wrap" title="textwrap.TextWrapper.wrap"><tt class="xref py py-meth docutils literal"><span class="pre">TextWrapper.wrap()</span></tt></a> method for additional details on how
<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><tt class="xref py py-func docutils literal"><span class="pre">wrap()</span></tt></a> behaves.</p>
</dd></dl>

<dl class="function">
<dt id="textwrap.fill">
<tt class="descclassname">textwrap.</tt><tt class="descname">fill</tt><big>(</big><em>text</em>, <em>width=70</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#textwrap.fill" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string containing the
wrapped paragraph.  <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><tt class="xref py py-func docutils literal"><span class="pre">fill()</span></tt></a> is shorthand for</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span>
</pre></div>
</div>
<p>In particular, <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><tt class="xref py py-func docutils literal"><span class="pre">fill()</span></tt></a> accepts exactly the same keyword arguments as
<a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><tt class="xref py py-func docutils literal"><span class="pre">wrap()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="textwrap.shorten">
<tt class="descclassname">textwrap.</tt><tt class="descname">shorten</tt><big>(</big><em>text</em>, <em>width</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#textwrap.shorten" title="Permalink to this definition">¶</a></dt>
<dd><p>Collapse and truncate the given <em>text</em> to fit in the given <em>width</em>.</p>
<p>First the whitespace in <em>text</em> is collapsed (all whitespace is replaced by
single spaces).  If the result fits in the <em>width</em>, it is returned.
Otherwise, enough words are dropped from the end so that the remaining words
plus the <tt class="xref py py-attr docutils literal"><span class="pre">placeholder</span></tt> fit within <tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s">&quot;Hello  world!&quot;</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="go">&#39;Hello world!&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s">&quot;Hello  world!&quot;</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span>
<span class="go">&#39;Hello [...]&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">textwrap</span><span class="o">.</span><span class="n">shorten</span><span class="p">(</span><span class="s">&quot;Hello world&quot;</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">placeholder</span><span class="o">=</span><span class="s">&quot;...&quot;</span><span class="p">)</span>
<span class="go">&#39;Hello...&#39;</span>
</pre></div>
</div>
<p>Optional keyword arguments correspond to the instance attributes of
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a>, documented below.  Note that the whitespace is
collapsed before the text is passed to the <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><tt class="xref py py-meth docutils literal"><span class="pre">fill()</span></tt></a>
function, so changing the value of <a class="reference internal" href="#textwrap.TextWrapper.tabsize" title="textwrap.TextWrapper.tabsize"><tt class="xref py py-attr docutils literal"><span class="pre">tabsize</span></tt></a>, <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><tt class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></tt></a>,
<a class="reference internal" href="#textwrap.TextWrapper.drop_whitespace" title="textwrap.TextWrapper.drop_whitespace"><tt class="xref py py-attr docutils literal"><span class="pre">drop_whitespace</span></tt></a>, and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><tt class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></tt></a> will have no effect.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="textwrap.dedent">
<tt class="descclassname">textwrap.</tt><tt class="descname">dedent</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#textwrap.dedent" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove any common leading whitespace from every line in <em>text</em>.</p>
<p>This can be used to make triple-quoted strings line up with the left edge of the
display, while still presenting them in the source code in indented form.</p>
<p>Note that tabs and spaces are both treated as whitespace, but they are not
equal: the lines <tt class="docutils literal"><span class="pre">&quot;</span>&nbsp; <span class="pre">hello&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;\thello&quot;</span></tt> are considered to have no
common leading whitespace.</p>
<p>For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
    <span class="c"># end first line with \ to avoid the empty line!</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span><span class="se">\</span>
<span class="s">    hello</span>
<span class="s">      world</span>
<span class="s">    &#39;&#39;&#39;</span>
    <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>          <span class="c"># prints &#39;    hello\n      world\n    &#39;</span>
    <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">dedent</span><span class="p">(</span><span class="n">s</span><span class="p">)))</span>  <span class="c"># prints &#39;hello\n  world\n&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="textwrap.indent">
<tt class="descclassname">textwrap.</tt><tt class="descname">indent</tt><big>(</big><em>text</em>, <em>prefix</em>, <em>predicate=None</em><big>)</big><a class="headerlink" href="#textwrap.indent" title="Permalink to this definition">¶</a></dt>
<dd><p>Add <em>prefix</em> to the beginning of selected lines in <em>text</em>.</p>
<p>Lines are separated by calling <tt class="docutils literal"><span class="pre">text.splitlines(True)</span></tt>.</p>
<p>By default, <em>prefix</em> is added to all lines that do not consist
solely of whitespace (including any line endings).</p>
<p>For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;hello</span><span class="se">\n\n</span><span class="s"> </span><span class="se">\n</span><span class="s">world&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">indent</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s">&#39;  &#39;</span><span class="p">)</span>
<span class="go">&#39;  hello\n\n \n  world&#39;</span>
</pre></div>
</div>
<p>The optional <em>predicate</em> argument can be used to control which lines
are indented. For example, it is easy to add <em>prefix</em> to even empty
and whitespace-only lines:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">indent</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="s">&#39;+ &#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">line</span><span class="p">:</span> <span class="k">True</span><span class="p">))</span>
<span class="go">+ hello</span>
<span class="go">+</span>
<span class="go">+</span>
<span class="go">+ world</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.3.</span></p>
</div>
</dd></dl>

<p><a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><tt class="xref py py-func docutils literal"><span class="pre">wrap()</span></tt></a>, <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><tt class="xref py py-func docutils literal"><span class="pre">fill()</span></tt></a> and <a class="reference internal" href="#textwrap.shorten" title="textwrap.shorten"><tt class="xref py py-func docutils literal"><span class="pre">shorten()</span></tt></a> work by creating a
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> instance and calling a single method on it.  That
instance is not reused, so for applications that process many text
strings using <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><tt class="xref py py-func docutils literal"><span class="pre">wrap()</span></tt></a> and/or <a class="reference internal" href="#textwrap.fill" title="textwrap.fill"><tt class="xref py py-func docutils literal"><span class="pre">fill()</span></tt></a>, it may be more efficient to
create your own <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> object.</p>
<p>Text is preferably wrapped on whitespaces and right after the hyphens in
hyphenated words; only then will long words be broken if necessary, unless
<a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><tt class="xref py py-attr docutils literal"><span class="pre">TextWrapper.break_long_words</span></tt></a> is set to false.</p>
<dl class="class">
<dt id="textwrap.TextWrapper">
<em class="property">class </em><tt class="descclassname">textwrap.</tt><tt class="descname">TextWrapper</tt><big>(</big><em>**kwargs</em><big>)</big><a class="headerlink" href="#textwrap.TextWrapper" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> constructor accepts a number of optional keyword
arguments.  Each keyword argument corresponds to an instance attribute, so
for example</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">(</span><span class="n">initial_indent</span><span class="o">=</span><span class="s">&quot;* &quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>is the same as</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">()</span>
<span class="n">wrapper</span><span class="o">.</span><span class="n">initial_indent</span> <span class="o">=</span> <span class="s">&quot;* &quot;</span>
</pre></div>
</div>
<p>You can re-use the same <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> object many times, and you can
change any of its options through direct assignment to instance attributes
between uses.</p>
<p>The <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> instance attributes (and keyword arguments to the
constructor) are as follows:</p>
<dl class="attribute">
<dt id="textwrap.TextWrapper.width">
<tt class="descname">width</tt><a class="headerlink" href="#textwrap.TextWrapper.width" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">70</span></tt>) The maximum length of wrapped lines.  As long as there
are no individual words in the input text longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a>,
<a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> guarantees that no output line will be longer than
<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a> characters.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.expand_tabs">
<tt class="descname">expand_tabs</tt><a class="headerlink" href="#textwrap.TextWrapper.expand_tabs" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">True</span></tt>) If true, then all tab characters in <em>text</em> will be
expanded to spaces using the <tt class="xref py py-meth docutils literal"><span class="pre">expandtabs()</span></tt> method of <em>text</em>.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.tabsize">
<tt class="descname">tabsize</tt><a class="headerlink" href="#textwrap.TextWrapper.tabsize" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">8</span></tt>) If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><tt class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></tt></a> is true, then all tab characters
in <em>text</em> will be expanded to zero or more spaces, depending on the
current column and the given tab size.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.3.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.replace_whitespace">
<tt class="descname">replace_whitespace</tt><a class="headerlink" href="#textwrap.TextWrapper.replace_whitespace" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">True</span></tt>) If true, after tab expansion but before wrapping,
the <a class="reference internal" href="#textwrap.wrap" title="textwrap.wrap"><tt class="xref py py-meth docutils literal"><span class="pre">wrap()</span></tt></a> method will replace each whitespace character
with a single space.  The whitespace characters replaced are
as follows: tab, newline, vertical tab, formfeed, and carriage
return (<tt class="docutils literal"><span class="pre">'\t\n\v\f\r'</span></tt>).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If <a class="reference internal" href="#textwrap.TextWrapper.expand_tabs" title="textwrap.TextWrapper.expand_tabs"><tt class="xref py py-attr docutils literal"><span class="pre">expand_tabs</span></tt></a> is false and <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><tt class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></tt></a> is true,
each tab character will be replaced by a single space, which is <em>not</em>
the same as tab expansion.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If <a class="reference internal" href="#textwrap.TextWrapper.replace_whitespace" title="textwrap.TextWrapper.replace_whitespace"><tt class="xref py py-attr docutils literal"><span class="pre">replace_whitespace</span></tt></a> is false, newlines may appear in the
middle of a line and cause strange output. For this reason, text should
be split into paragraphs (using <a class="reference internal" href="stdtypes.html#str.splitlines" title="str.splitlines"><tt class="xref py py-meth docutils literal"><span class="pre">str.splitlines()</span></tt></a> or similar)
which are wrapped separately.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.drop_whitespace">
<tt class="descname">drop_whitespace</tt><a class="headerlink" href="#textwrap.TextWrapper.drop_whitespace" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">True</span></tt>) If true, whitespace at the beginning and ending of
every line (after wrapping but before indenting) is dropped.
Whitespace at the beginning of the paragraph, however, is not dropped
if non-whitespace follows it.  If whitespace being dropped takes up an
entire line, the whole line is dropped.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.initial_indent">
<tt class="descname">initial_indent</tt><a class="headerlink" href="#textwrap.TextWrapper.initial_indent" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">''</span></tt>) String that will be prepended to the first line of
wrapped output.  Counts towards the length of the first line.  The empty
string is not indented.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.subsequent_indent">
<tt class="descname">subsequent_indent</tt><a class="headerlink" href="#textwrap.TextWrapper.subsequent_indent" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">''</span></tt>) String that will be prepended to all lines of wrapped
output except the first.  Counts towards the length of each line except
the first.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.fix_sentence_endings">
<tt class="descname">fix_sentence_endings</tt><a class="headerlink" href="#textwrap.TextWrapper.fix_sentence_endings" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">False</span></tt>) If true, <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> attempts to detect
sentence endings and ensure that sentences are always separated by exactly
two spaces.  This is generally desired for text in a monospaced font.
However, the sentence detection algorithm is imperfect: it assumes that a
sentence ending consists of a lowercase letter followed by one of <tt class="docutils literal"><span class="pre">'.'</span></tt>,
<tt class="docutils literal"><span class="pre">'!'</span></tt>, or <tt class="docutils literal"><span class="pre">'?'</span></tt>, possibly followed by one of <tt class="docutils literal"><span class="pre">'&quot;'</span></tt> or <tt class="docutils literal"><span class="pre">&quot;'&quot;</span></tt>,
followed by a space.  One problem with this is algorithm is that it is
unable to detect the difference between &#8220;Dr.&#8221; in</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">Dr</span><span class="o">.</span> <span class="n">Frankenstein</span><span class="s">&#39;s monster [...]</span>
</pre></div>
</div>
<p>and &#8220;Spot.&#8221; in</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">See</span> <span class="n">Spot</span><span class="o">.</span> <span class="n">See</span> <span class="n">Spot</span> <span class="n">run</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span>
</pre></div>
</div>
<p><a class="reference internal" href="#textwrap.TextWrapper.fix_sentence_endings" title="textwrap.TextWrapper.fix_sentence_endings"><tt class="xref py py-attr docutils literal"><span class="pre">fix_sentence_endings</span></tt></a> is false by default.</p>
<p>Since the sentence detection algorithm relies on <tt class="docutils literal"><span class="pre">string.lowercase</span></tt> for
the definition of &#8220;lowercase letter,&#8221; and a convention of using two spaces
after a period to separate sentences on the same line, it is specific to
English-language texts.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.break_long_words">
<tt class="descname">break_long_words</tt><a class="headerlink" href="#textwrap.TextWrapper.break_long_words" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">True</span></tt>) If true, then words longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a> will be
broken in order to ensure that no lines are longer than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a>.  If
it is false, long words will not be broken, and some lines may be longer
than <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a>.  (Long words will be put on a line by themselves, in
order to minimize the amount by which <a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a> is exceeded.)</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.break_on_hyphens">
<tt class="descname">break_on_hyphens</tt><a class="headerlink" href="#textwrap.TextWrapper.break_on_hyphens" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">True</span></tt>) If true, wrapping will occur preferably on whitespaces
and right after hyphens in compound words, as it is customary in English.
If false, only whitespaces will be considered as potentially good places
for line breaks, but you need to set <a class="reference internal" href="#textwrap.TextWrapper.break_long_words" title="textwrap.TextWrapper.break_long_words"><tt class="xref py py-attr docutils literal"><span class="pre">break_long_words</span></tt></a> to false if
you want truly insecable words.  Default behaviour in previous versions
was to always allow breaking hyphenated words.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.max_lines">
<tt class="descname">max_lines</tt><a class="headerlink" href="#textwrap.TextWrapper.max_lines" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">None</span></tt>) If not <tt class="docutils literal"><span class="pre">None</span></tt>, then the output will contain at most
<em>max_lines</em> lines, with <em>placeholder</em> appearing at the end of the output.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.placeholder">
<tt class="descname">placeholder</tt><a class="headerlink" href="#textwrap.TextWrapper.placeholder" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="docutils literal"><span class="pre">'</span> <span class="pre">[...]'</span></tt>) String that will appear at the end of the output
text if it has been truncated.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<p><a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> also provides some public methods, analogous to the
module-level convenience functions:</p>
<dl class="method">
<dt id="textwrap.TextWrapper.wrap">
<tt class="descname">wrap</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#textwrap.TextWrapper.wrap" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most
<a class="reference internal" href="#textwrap.TextWrapper.width" title="textwrap.TextWrapper.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a> characters long.  All wrapping options are taken from
instance attributes of the <a class="reference internal" href="#textwrap.TextWrapper" title="textwrap.TextWrapper"><tt class="xref py py-class docutils literal"><span class="pre">TextWrapper</span></tt></a> instance.  Returns a list
of output lines, without final newlines.  If the wrapped output has no
content, the returned list is empty.</p>
</dd></dl>

<dl class="method">
<dt id="textwrap.TextWrapper.fill">
<tt class="descname">fill</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#textwrap.TextWrapper.fill" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string
containing the wrapped paragraph.</p>
</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="difflib.html"
                        title="previous chapter">6.3. <tt class="docutils literal"><span class="pre">difflib</span></tt> &#8212; Helpers for computing deltas</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="unicodedata.html"
                        title="next chapter">6.5. <tt class="docutils literal"><span class="pre">unicodedata</span></tt> &#8212; Unicode Database</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/textwrap.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="unicodedata.html" title="6.5. unicodedata — Unicode Database"
             >next</a> |</li>
        <li class="right" >
          <a href="difflib.html" title="6.3. difflib — Helpers for computing deltas"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="text.html" >6. Text Processing Services</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Feb 26, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>